<?php

namespace Admin\Controller;

use Admin\Controller\AdminController;
use Admin\Model\NewsModel;
use Admin\Model\NewsCateModel;

/**
 * @author wangnaixin
 * @todo 内容模块
 * @menu 内容管理
 * @author wangnaixin
 * @menuAuto index
 * @order 210
 * @icon article.png
 */
class NewsController extends AdminController {

    //put your code here

    public $news;
    public $newsCate;

    function __construct() {
        parent::__construct();
        $this->news = new NewsModel();
        $this->newsCate = new NewsCateModel();
    }

    /**
     * @todo 内容列表
     * @menu 内容列表
     * @order 1
     */
    public function index() {
        
        
        $this->setForm();
        $where = array();
        if(IS_POST){
            $cate_id = I('cate_id');
            if(!empty($cate_id)){
                $where['cate_id'] = array('in',$this->newsCate->getChild($cate_id));
            }
            $title = I('title');
            if(!empty($title)){
                $where['title'] = array('like',"%".$title."%");
            }
        }
        $newsList = $this->news->getNewsList($where);
        $this->assign("newsList",$newsList);
        $cateList = $this->newsCate->getList();
        
        $this->assign("cateList", $cateList);
        $this->display();
    }

    /**
     * @todo 分类列表
     * @menu 分类列表
     * @order 2
     */
    public function cateList() {
        $cateList = $this->newsCate->getList();
        
        $this->assign("cateList", $cateList);
        $this->display("Building/NewsCate/index");
    }

    /**
     * @todo 添加新闻
     */
    public function addNews() {
        $cateList = $this->newsCate->getList();
        $this->assign("cateList", $cateList);
        $this->assign("pageName","新闻添加");
        $this->display();
    }
    
    /**
     * @todo 添加新闻操作
     */
    public function doAddNews(){
        if ($data = $this->news->create()) {
            $newsId = I('id');
            unset($data['id']);
            if(empty($newsId)){
            	if ($this->news->add($data)) {
                    $this->ajaxReturn(array("status"=>true,'msg'=>'新闻添加成功'));
                } else {
                    $this->ajaxReturn(array("status"=>true,'msg'=>'新闻添加失败'));
                }
            }else{
            	if ($this->news->where("id = ".$newsId)->save($data)) {
                    $this->ajaxReturn(array("status"=>true,'msg'=>'新闻修改成功'));
                } else {
                    $this->ajaxReturn(array("status"=>true,'msg'=>'新闻修改失败'));
                }
            }
            
        } else {
            $this->ajaxReturn(array("status"=>true,'msg'=>'新闻添加失败'));
        }
    }

    /**
     * @todo 添加新闻分类
     */
    public function addNewsCate() {
        $cateList = $this->newsCate->getList([]);
        $this->assign("cateList", $cateList);
        $this->assign("pageName","新闻分类添加");
        $this->display("Building/NewsCate/add");
    }

    /**
     * @todo 添加新闻分类操作
     */
    public function doAddNewsCate() {
        if ($this->newsCate->create()) {
            $cateId = I('id');
            if(empty($cateId)){
                if ($this->newsCate->add()) {
                    $this->ajaxReturn(array("status"=>true,'msg'=>'新闻分类添加成功'));
                } else {
                    $this->ajaxReturn(array("status"=>true,'msg'=>'新闻分类添加失败'));
                }
            }else{
                if ($this->newsCate->where("id = ".$cateId)->save()) {
                    $this->ajaxReturn(array("status"=>true,'msg'=>'新闻分类修改成功'));
                } else {
                    $this->ajaxReturn(array("status"=>true,'msg'=>'新闻分类修改失败'));
                }
            }
            
        } else {
            $this->ajaxReturn(array("status"=>true,'msg'=>'新闻分类添加失败'));
        }
    }
    /**
     * @todo 修改新闻分类
     */
    public function editNewsCate(){
        $cateId = I('cateId');
        $cateInfo = $this->newsCate->getCateInfoById($cateId);
        $this->assign("cateInfo",$cateInfo);
        $cateList = $this->newsCate->getList(array('pid' => 0));
        $this->assign("cateList", $cateList);
        $this->assign("pageName","新闻分类修改");
        $this->display("Building/NewsCate/add");
    }
    
    /**
     * @todo 修改新闻
     */
    
    public function editNews(){
        $newsId = I('newsId');
        $newsInfo = $this->news->getNewsInfoById($newsId);
        $this->assign("newsInfo",$newsInfo);
        $cateList = $this->newsCate->getList();
        $this->assign("cateList", $cateList);
        $this->assign("pageName","新闻修改");
        $this->display("Building/News/addNews");
    }
    
    /**
     * @todo 删除新闻分类
     */
    public function delNewsCate(){
        $cateId = I('cateId');
        $this->newsCate->delCateById($cateId);
        $this->ajaxReturn(true);
    }
    
    /**
     * @todo 删除新闻
     */
    public function delNews(){
        $newsId = I('newsId');
        $this->news->delNewsById($newsId);
        $this->ajaxReturn(true);
    }

}
